using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuickLaunch
{
    public partial class ShortcutTextBox : TextBox
    {
        const int WM_KEYDOWN = 0x100;
        const int WM_SYSKEYDOWN = 0x104;

        public ShortcutTextBox()
        {
            InitializeComponent();

            this.BackColor = Color.Lavender;
        }
        
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
            {
                // Ctrl + key
                if (keyData >= (Keys)Shortcut.Ctrl0 && keyData <= (Keys)Shortcut.CtrlF12)
                {
                    this.Text = ((Shortcut)keyData).ToString().Replace("Ctrl", "Ctrl + ");
                }
                // Alt + key
                else if ((keyData >= (Keys)Shortcut.Alt0 && keyData <= (Keys)Shortcut.Alt9) ||
                    (keyData >= (Keys)Shortcut.AltF1 && (keyData >= (Keys)Shortcut.AltF12)))
                {
                    this.Text = ((Shortcut)keyData).ToString().Replace("Alt", "Alt + ");
                }
                // Ctrl + Shift + key
                else if (keyData >= (Keys)Shortcut.CtrlShift0 && keyData <= (Keys)Shortcut.CtrlShiftF12)
                {
                    this.Text = ((Shortcut)keyData).ToString().Replace("CtrlShift", "Ctrl + Shift + ");
                }
                // single key
                else if ((keyData >= Keys.D0 && keyData <= Keys.Z) ||
                    keyData >= Keys.F1 && keyData <= Keys.F24)
                {
                    this.Text = "Ctrl + " + keyData.ToString();
                }
                // Back or Delete
                else if (keyData == Keys.Delete || keyData == Keys.Back)
                {
                    this.Text = "";
                }

                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: Add custom paint code here

            // Calling the base class OnPaint
            base.OnPaint(pe);
        }
    }
}
